package stz.flash.option;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;

import stz.flash.main.FlashTimer;


public class TimerDurationController {

	private static Option option = Option.getIns();
	private static int currentIndex;
	private static Vector vec = new Vector(11);
	
	private static final TimerDurationController instance = new TimerDurationController();
	
	private TimerDurationController() {
		super();
	
		
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_1_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_2_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_3_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_4_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_5_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_6_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_7_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_8_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_9_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_10_MINUTE);
		vec.addElement(TimerDurationChoiceField.INTEGER_OF_INTEGER_MAX_VALUE);
		
		currentIndex = vec.indexOf(getTimerDuration());
	}
	
	public static TimerDurationController getIns(){
		return instance;
	}
	
	
	public static void increaseTimerDuration() {
		try {
			
			currentIndex++;
			checkIndexOutOfBounds();
			updateOptionAndChoiceField();
			FlashTimer.getIns().stopAndStart(FlashTimer.STOP_AND_START_timer_reset);
		
		} catch (Exception e) {
		}
	}
	public static void decreaseTimerDuration() {
		try {
			
			currentIndex--;
			checkIndexOutOfBounds();
			updateOptionAndChoiceField();
			FlashTimer.getIns().stopAndStart(FlashTimer.STOP_AND_START_timer_reset);
			
		} catch (Exception e) {
		}
	}
	private static void updateOptionAndChoiceField() {
		option.setTimerDuration((Integer) vec.elementAt(currentIndex));
		TimerDurationChoiceField.getIns().updateSelected();
	}
	private static void checkIndexOutOfBounds() {
		if (currentIndex < 0)
			currentIndex = 0;
		if (currentIndex > vec.size() - 1)
			currentIndex = vec.size() - 1;
	}
	
	
	public void setTimerDuration(Integer duration) {
		currentIndex = vec.indexOf(duration);
		option.setTimerDuration(duration);
	}
	public Integer getTimerDuration() {
		return option.getTimerDuration();
	}
}